package com.hyt.it.ogt.ks.config;

import java.io.IOException;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import com.hyt.it.ogt.ks.config.canal.CanalMonitor;
import org.springframework.stereotype.Component;

import com.hyt.it.ogt.ks.websocket.CandidateWebSocketService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 刘恒活
 * @since 2021/03/17
 */

@Component
@Slf4j
public class ShutdownSystem {

    @Resource
    private CandidateWebSocketService candidateWebSocketService;
    @Resource
    private CanalMonitor canalMonitor;

    @PreDestroy
    public void stop() {
        log.info("========服务器关闭开始=======");
        this.closeWebsocket();
        canalMonitor.close();
        log.info("========服务器关闭结束=======");
    }

    private void closeWebsocket() {
        candidateWebSocketService.getOnlineSession().forEach((k, v) -> {
            log.info("========closeWebsocket：{}=======", v.getId());
            try {
                v.close();
            } catch (IOException e) {
                log.error("", e);
            }
        });
    }
}
